require 'Person.rb'

#
# CPL Assignment 2
# Tim Caljé, Jeroen De Raedt & Jorick Van Aken
#
class BookingInterpreter
  
  def initialize(bookingDao)
    @bookingDao =  bookingDao
    @destinations = Array.new
    @type="F"
    @date = ""
    @flights = Array.new
    @persons = Array.new
    @bookingDetailIds = Array.new
    @bookingIds = Array.new
  end
  
  def person(first,sur,gender)
    @persons.push(Person.new(first,sur,gender))
    self
  end
  
  def flightNr(flightNr)
    @flights.push(flightNr)
    self
  end
  
  def date(date)
    if(date.length == 10)
      @date = date
    else
      raise Exception, 'Not a valid date.'
    end
    self
  end
  
  def buisnessClass
    if @type == "F"
      @type = "B"
    else
      raise Exception, "You can only use 1 type."
    end
    self
  end
  
  def firstClass
    if @type == "F"
      @type = "F"
    else
      raise Exception, "You can only use 1 type."
    end
    self
  end
  
  def economyClass
    if @type == "F"
      @type = "E"
    else
      raise Exception, "You can only use 1 type."
    end
    self
  end
  
  def bookingId(id)
    if(id.length == 32)
      @bookingIds.push(id)
    else
      raise Exception, "Bookingids should have 32 characters."
    end
    self
  end
  
  def hold
    if(@flights.length == 0)
      raise Exception, 'No flights specified.'
    end
    if(@date == '')
      raise Exception, 'No date specified.'
    end
    if(@persons.length == 0)
      raise Exception, 'No persons specified.'
    else
      @bookingDao.holdFlights(@flights,@date,@persons,@type)
    end
  end
    
  def book
    if(@bookingIds.length ==0)
      results = hold()
      @bookingDao.bookFlights(results)
    else
      @bookingDao.bookFlights(@bookingIds)
    end
  end
  
  def cancel
    if(@bookingIds.length==0)
      raise Exception, 'No bookingIds specified.'
    else
      @bookingDao.cancelHolds(@bookingIds)
    end
  end
  
  def details(bookingID)
    @bookingDetailIds.push(bookingID)
    self
  end
  
  def query
     @bookingDao.getBookings(@bookingDetailIds)
   end
  
end